Page({
    data: {
        list: [],
        arr: [100, 200, 300],
        activeIndex: 0,
        heightSet: [],
        cart: [],
        tag: false
    },
    async onLoad() {
        wx.cloud.init()
        let result: any = await wx.cloud.callFunction({ name: "dish-manage" }).catch(err => console.error(err))
        let list = result.result.data
        this.setData({
            list: list
        })
        //获取右侧高度
        const query = wx.createSelectorQuery()
        query.selectAll('.rightItem').boundingClientRect()
        let heightSet: any = []
        query.exec(res => {
            heightSet = res[0].map(item => {
                return item.top
            })
            this.setData({
                heightSet: heightSet
            })
        })

    },

    //点击左侧
    leftTap(e: any) {
        let { index } = e.target.dataset;
        this.setData({
            activeIndex: index,
            scrollInto: this.data.list[index]['cid']
        })


    },
    //右侧滚动
    rightScroll(e: any) {
        let activeIndex = this.data.activeIndex;

        if (e.detail.scrollTop > this.data.heightSet[activeIndex + 1]) {
            this.setData({
                activeIndex: activeIndex += 1
            })
        }
        if (e.detail.scrollTop < this.data.heightSet[activeIndex - 1]) {
            this.setData({
                activeIndex: activeIndex -= 1
            })
        }
    },
    //添加购物车
    add(e: any) {
        let { item } = e.currentTarget.dataset;
        this.data.cart.push(item)
        this.setData({
            cart: this.data.cart
        })
    },
    //展示购物车
    showCart() {
        this.setData({
            tag: true
        })
    },
    hideCart() {
        this.setData({
            tag: false
        })
    }
})